package org.emergency.framework.dao.oa.Impl;

import java.util.List;

import org.emergency.framework.dao.HibernateBaseDao;
import org.emergency.framework.dao.oa.ITbDepartmentDAO;
import org.emergency.framework.model.TbDepartment;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Repository;

/**
 * 
 * @author Administrator
 *
 */
@Repository("tbDepartmentDAO")
public class TbDepartmentDAO extends HibernateBaseDao<TbDepartment> implements ITbDepartmentDAO{
	private static final Logger log = LoggerFactory
			.getLogger(TbDepartmentDAO.class);

	protected void initDao() {
		// do nothing
	}


	public TbDepartment findById(java.lang.String id) {
		log.debug("getting TbDepartment instance with id: " + id);
		try {
			TbDepartment instance = (TbDepartment) getHibernateTemplate().get(
					"org.dao.TbDepartment", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<TbDepartment> findByExample(TbDepartment instance) {
		log.debug("finding TbDepartment instance by example");
		try {
			List<TbDepartment> results = (List<TbDepartment>) getHibernateTemplate()
					.findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TbDepartment instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TbDepartment as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public TbDepartment merge(TbDepartment detachedInstance) {
		log.debug("merging TbDepartment instance");
		try {
			TbDepartment result = (TbDepartment) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(TbDepartment instance) {
		log.debug("attaching dirty TbDepartment instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(TbDepartment instance) {
		log.debug("attaching clean TbDepartment instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static TbDepartmentDAO getFromApplicationContext(ApplicationContext ctx) {
		return (TbDepartmentDAO) ctx.getBean("TbDepartmentDAO");
	}
}